#include "SudokuCellSet.h"
#include "SudokuCell.h"

SudokuCellSet::SudokuCellSet(SetType type) : QList<SudokuCell*>(),
mType(type),
mFixedType(type != Unknown)
{
}

void SudokuCellSet::append(SudokuCell* const& cell)
{
    if(!mFixedType)
    {
        mType = Unknown;
    }
    QList<SudokuCell*>::append(cell);
}

bool SudokuCellSet::containsSet(const SudokuCellSet& other) const
{
    if(other.size() == 0)
        return false;

    for(int i=0;i<other.size();++i)
        if(!this->contains(other.at(i)))
            return false;

    return true;
}

bool SudokuCellSet::isCol()
{
    if(Unknown == mType)
        checkType();

    return (Col & mType);
}

bool SudokuCellSet::isRow()
{
    if(Unknown == mType)
        checkType();

    return (Row & mType);
}

bool SudokuCellSet::isBox()
{
    if(Unknown == mType)
        checkType();

    return (Box & mType);
}

QDebug operator<<(QDebug dbg, const SudokuCellSet& set)
{
    for(int i=0;i<set.size();++i)
    {
        dbg.nospace() << *set.at(i) << '.';
    }

    return dbg.maybeSpace();
}


void SudokuCellSet::checkType()
{
    if(isEmpty())
        return;

    mType = Unknown;

    // TODO it should check also count of cells

    int colInd = this->first()->getCol();
    int rowInd = this->first()->getRow();
    int boxInd = this->first()->getBoxIndex();

    bool row = true;
    bool col = true;
    bool box = true;

    for(int cellInd = 1;cellInd<size();++cellInd)
    {
        if(this->at(cellInd)->getRow() != rowInd)
            row = false;
        if(this->at(cellInd)->getCol() != colInd)
            col = false;
        if(this->at(cellInd)->getBoxIndex() != boxInd)
            box = false;
    }

    if(row)
    {
        mType |= Row;
    }
    if(col)
    {
        mType |= Col;
    }
    if(box)
    {
        mType |= Box;
    }

    if(mType == Unknown)
    {
        mType = Other;
    }
}
